package com.sanpu.kxe.business.main.fragment;

import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.BuildConfig;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.business.card.provider.ICardProvider;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.business.member.provider.IMemberProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.view.ShapeImageView;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.SystemBarHelper;
import com.sanpu.kxe.utils.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：我的
 * Created by 薛勇军 on 2018/7/9.
 */

public class MyFragment extends BaseFragment {

    @Bind(R.id.avatar)
    ShapeImageView avatar;
    @Bind(R.id.gradeRtv)
    RoundTextView gradeRtv;
    @Bind(R.id.name)
    TextView vName;
    @Bind(R.id.accountTv)
    TextView accountTv;
    @Bind(R.id.versionTv)
    TextView versionTv;
    @Bind(R.id.rightBadge)
    RoundTextView rightBadge;
    @Bind(R.id.authStatusTv)
    TextView authStatusTv;
    @Bind(R.id.isOriginalVipRtv)
    RoundTextView isOriginalVipRtv;

    @Bind(R.id.headerView)
    RelativeLayout headerView;

    private String chkSts;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) headerView.getLayoutParams();
        params.setMargins(0, barHeight, 0, 0);
        headerView.setLayoutParams(params);
        versionTv.setText(BuildConfig.VERSION_NAME);
    }

    @OnClick({R.id.user_center_layout, R.id.my_bank, R.id.my_up_lvl, R.id.my_service,
            R.id.my_about, R.id.my_setting, R.id.rightRl, R.id.my_auth})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_center_layout:
                MainOpenProvider.getInstance().openUserCenterActivity(getContext());
                break;
            case R.id.my_bank:
                ICardProvider cardProvider = PublicProviderUtil.getProvider(ICardProvider.class);
                if (cardProvider != null) {
                    cardProvider.openMyBankListActivity(getContext());
                }
                break;
            case R.id.my_up_lvl:
                IMemberProvider provider = PublicProviderUtil.getProvider(IMemberProvider.class);
                if (provider != null) {
                    provider.openMemberProductRateActivity(getContext());
                }
                break;
            case R.id.my_setting:
                MainOpenProvider.getInstance().openSettingCenterActivity(getContext());
                break;
            case R.id.my_about:
                break;
            case R.id.my_partner:
                MainOpenProvider.getInstance().openPartnerActivity(getContext());
                break;
            case R.id.my_service:
                MainOpenProvider.getInstance().openAboutActivity(getContext());
                break;
            case R.id.rightRl:
                MainOpenProvider.getInstance().openNoticeActivity(getContext());
                break;
            case R.id.my_auth:
                if (TextUtils.equals(CommonConfig.AUTHEN_AUDIT, chkSts)) {
                    ToastUtil.showTextViewPrompt("正在审核中，请耐心等待");
                    return;
                }
                MainOpenProvider.getInstance().openCertificationActivity(getContext());
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            showViewByData(output);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEntity(UserCenterOutput event) {
        if (event != null) {
            showViewByData(event);
        }
    }

    /**
     * 根据数据显示view
     */
    private void showViewByData(UserCenterOutput output) {
        if (output == null) return;
        chkSts = output.getChkSts();
        vName.setText(TextUtils.isEmpty(output.getNickName()) ? output.getUserName() : output.getNickName());
        if (!TextUtils.isEmpty(output.getUserName())) {
            accountTv.setText("账号：" + output.getUserName() + "");
        }
        if (!TextUtils.isEmpty(output.getLevelImagePath())) {
            JKImageLoader.getInstance().displayImage(getContext(),
                    UrlConstants.getBaseUrl() + output.getLevelImagePath(), avatar);
        }
        if (!TextUtils.isEmpty(output.getLevelName())) {
            gradeRtv.setText(output.getLevelName());
        }
        if (output.hasMsgRead()) {
            rightBadge.setVisibility(View.VISIBLE);
        } else {
            rightBadge.setVisibility(View.GONE);
        }
        if (output.isOriginalVip()) {
            isOriginalVipRtv.setVisibility(View.VISIBLE);

        } else {
            isOriginalVipRtv.setVisibility(View.GONE);
        }
        setAuthText(chkSts);
    }

    /**
     * 获取认证状态
     *
     * @param chkSts 认证状态
     */
    private void setAuthText(String chkSts) {
        switch (chkSts) {
            case CommonConfig.AUTHEN_NOCOMPLETION:
                authStatusTv.setText("(资料待补全)");
                authStatusTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_ff3f40));
                break;
            case CommonConfig.AUTHEN_PASS:
                authStatusTv.setText("(已认证)");
                authStatusTv.setTextColor(ContextCompat.getColor(getContext(), R.color.color_base));
                break;
            case CommonConfig.AUTHEN_AUDIT:
                authStatusTv.setText("(审核中)");
                authStatusTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_7BCE72));
                break;
            case CommonConfig.AUTHEN_NOPASS:
                authStatusTv.setText("(审核未通过)");
                authStatusTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_ff3f40));
                break;
            default:
                authStatusTv.setText("(未认证)");
                authStatusTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_ff3f40));
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }
}
